<style>
	@import "../../../common/css/adminHome.css";
</style>
<template>
	<view>
		<view class="personTop">
			<!-- <image class="personTop_bg" src="../../../static/personBg.png" mode="aspectFill"></image> -->
			<view class="personTop_info">
				<avatar :size="104" :marginTop="0" :src="userInfo.avatar" :marginLeft="46" style="float: left;"></avatar>
				<view class="personTop_con">
					<view class="personTop_info_name">{{userInfo.name}}</view>
					
				</view>
				<view class="personTop_setPwd">
					<view class="personTop_addAccount" @click="addAccount" v-if=" adminType == 2">创建账号</view>
					<view class="personTop_addAccount" @click="showEditPwd(userInfo.userId)">修改密码</view>
				</view>
				<view class="personTop_info_account">主账号:{{userInfo.account}}</view>
			</view>
			<view class="personTop_assets">
				<view class="personTop_assets_left">
					<view class="personTop_assets_left_title">我的资产(元)</view>
					<view class="personTop_assets_left_money">
						<view class="personTop_assets_left_money_num">¥{{userInfo.totalAssets}}</view>
						<view class="personTop_assets_left_cash">
							<text class="personTop_assets_left_cash_title">收益额</text>
							<text class="personTop_assets_left_cash_num">¥{{userInfo.extractableAssets}}</text>
						</view>
					</view>
				</view>
				<view class="personTop_assets_cash_btn" @click="withdrawal()">
					提现
				</view>
			</view>
		</view>
		<view class="personContent">
			<view class="yinying"></view>
			<view class="personContent_achievement">
				<view class="achievement_info">
					<view class="achievement_info_top">
						<view class="achievement_info_top_date">
							<timePicker
								ref="times"
							    showType="yearToMonth"
								:userId="userInfo.userId"
							    @btnConfirm="btnConfirm" 
							    @btnCancel="btnCancel"
							>
								<text class="achievement_info_top_dateTime" >{{nowUserDataSheet.date.year}}/{{nowUserDataSheet.date.month}}</text>
							</timePicker>
							
							<image class="achievement_info_top_img" src="../../../static/down2.png" mode="aspectFill"></image>
						</view>
						<view class="achievement_info_top_profit">
							<text class="achievement_info_top_profit_title">收益(元)</text>
							<text class="achievement_info_top_profit_num">{{nowUserDataSheet.order.profitFee}}</text>
						</view>
						<view class="achievement_info_top_order">
							<text class="achievement_info_top_order_title">完成订单(个)</text>
							<text class="achievement_info_top_order_num">{{nowUserDataSheet.order.completedOrders}}</text>
						</view>
						<view class="achievement_info_top_refuse">
							<text class="achievement_info_top_refuse_title">拒绝订单(个)</text>
							<text class="achievement_info_top_refuse_num">{{nowUserDataSheet.order.rejectedOrders}}</text>
						</view>
					</view>
					<view class="achievement_info_left">
						<image class="achievement_info_left_img" src="../../../static/pf_star.png" mode="aspectFill"></image>
					</view>
					<view class="achievement_info_right">
						<jpCharts 
						:list="nowUserDataSheet.evaluation.xList"
						:yList="nowUserDataSheet.evaluation.yList"
						:Y="Charts.Y" 
						:X="Charts.X" 
						:keyId="Charts.keyId" 
						:width="Charts.width" 
						:btColor="Charts.btColor"
						:bgColor="Charts.bgColor" 
						:height="Charts.height" 
						:canClick="Charts.canClick" 
						:x_width="Charts.x_width" 
						:showZ="Charts.showZ"
						:proportion="Charts.proportion" 
						:checkedColor="Charts.checkedColor" 
						:scrollLeft="100"></jpCharts>
					</view>
				</view>
			</view>
		</view>
		<view class="sonPerson">
			<z-paging ref="paging"
			@query="queryList" 
			:list.sync="userDataSheetList" 
			>
				<view class="sonPersonItem" v-for="(item,index) in userDataSheetList" :key="index">
					<view class="sonTop">
						<avatar :size="112" :marginTop="26" :marginLeft="30" :src="item.userInfo.avatar" style="float: left;"></avatar>
						<view class="son_user">
							<view class="son_user_name">{{item.userInfo.name}}</view>
							<view class="son_user_account">主账号:{{item.userInfo.account}}</view>
						</view>
						<view class="son_user_pwd" @click="showEditPwd(item.userInfo.id)">修改密码</view>
						<view class="son_user_edit" @click="showEditUser(item.userInfo.id)">编辑</view>
					</view>
					<view class="achievement_info">
						<view class="achievement_info_top">
							<view class="achievement_info_top_date">
								<timePicker
								    showType="yearToMonth"
									:userId="item.userInfo.id"
									:itemIndexs="index"
								    @btnConfirm="btnConfirms" 
								    @btnCancel="btnCancel"
								>
									<text class="achievement_info_top_dateTime">{{item.orderJson.date.year}} / {{item.orderJson.date.month}}</text>
								</timePicker>
								
								<image class="achievement_info_top_img" src="../../../static/down2.png" mode="aspectFill"></image>
							</view>
							<view class="achievement_info_top_profit">
								<text class="achievement_info_top_profit_title">收益(元)</text>
								<text class="achievement_info_top_profit_num">{{item.orderJson.order.profitFee}}</text>
							</view>
							<view class="achievement_info_top_order">
								<text class="achievement_info_top_order_title">完成订单(个)</text>
								<text class="achievement_info_top_order_num">{{item.orderJson.order.completedOrders}}</text>
							</view>
							<view class="achievement_info_top_refuse">
								<text class="achievement_info_top_refuse_title">拒绝订单(个)</text>
								<text class="achievement_info_top_refuse_num">{{item.orderJson.order.rejectedOrders}}</text>
							</view>
						</view>
						<view class="achievement_info_left">
							<image class="achievement_info_left_img" src="../../../static/pf_star.png" mode="aspectFill"></image>
						</view>
						<view class="achievement_info_right">
							<jpCharts 
							:list="item.orderJson.evaluation.xList"
							:yList="item.orderJson.evaluation.yList"
							:Y="Charts.Y"
							:X="Charts.X"
							:keyId="Charts.keyId"
							:width="Charts.width"
							:btColor="Charts.btColor"
							:bgColor="Charts.bgColor"
							:height="Charts.height"
							:canClick="Charts.canClick"
							:x_width="Charts.x_width"
							:showZ="Charts.showZ"
							:proportion="Charts.proportion"
							:checkedColor="Charts.checkedColor"
							:scrollLeft="100"></jpCharts>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<createAccount v-if="isShowCreateAccount" @submitCreate="submitCreate" @cancelCreate="cancelCreate"></createAccount>
		<editPwd v-if="isShowEditPwd" :userId="editUserId" @cancelEditPwd="cancelEditPwd" @submitEditPwd="submitEditPwd"></editPwd>
		<editUser v-if="isShowEditUser" :userId="editUserId" @cancelEditUser="cancelEditUser" @submitEditUser="submitEditUser"></editUser>
		<tabBarAdmin :pagePath="'/pages/admin/personManage/personManage'"></tabBarAdmin>
		<view class="exitAccount" @click="exitAccount">
			退出账号
		</view>
		<view class="backHome" @click="goToHome">
			返回首页
		</view>
	</view>
</template>

<script>
	import avatar from '@/components/user/avatar.vue'
	import jpCharts from '@/components/jp-charts/index.vue'
	import createAccount from '@/components/show-model/show-createAccount/show-createAccount.vue'
	import editPwd from '@/components/show-model/show-editPwd/show-editPwd.vue'
	import editUser from '@/components/show-model/show-editUser/show-editUser.vue'
	import timePicker from '@/components/wing-time-selector/wing-time-selector.vue'
	import base from '@/api/base'
	export default {
		components:{
			avatar,jpCharts,createAccount,editPwd,editUser,timePicker
		},
		data() {
			return {
				//y轴配置 value在list中的键 showY是否一直显示数据 size字大小 units文字后缀
				Charts: { 
					 Y: {
						 value: 'value',
						 showY: true,
						 size: 20,
						 units: '',
						 color:'#6C6C6C'
					 },
					 X: {
						 value: 'keyw',
						 showX: true,
						 size: 20,
						 units: '',
						 color:'#6C6C6C'
					 },
					 keyId: 'keyw',
					 list: [],
					 items: {},
					 width: 496,
					 height: 200,
					 x_width: 100,
					 proportion: 30, //宽度占比
					 showZ: true, //是否显示线条
					 checkedColor: '#007aff',
					 canClick: true, //不可以点击
					 bgColor: '#2D8FF2',
					 btColor:'#63D4F6',
					 scrollLeft: 0
				},
				list: [
					{ keyw: '5星', value: 528}, 
					{ keyw: '4星', value: 413}, 
					{ keyw: '3星', value: 362}, 
					{ keyw: '2星', value: 225}, 
					{ keyw: '1星', value: 69},
				],
				yList:[
					{value:600},{value:400},{value:200},{value:0}
				],
				dataList: [],
				isShowCreateAccount:false,
				isShowEditPwd:false,
				isShowEditUser:false,
				userInfo:{},
				editUserId:0,
				nowUserDataSheet:{},
				userDataSheetList:[],
				chooseDateUserId:0,
				chooseDateYear:'',
				chooseDateMonth:'',
				changeDateUserId:0,
				changeDateYear:'',
				changeDateMonth:'',
			}
		},
		beforeMount() {
			this.getUserInfo() //获取用户当前信息
			this.getUserMoney()//获取用户金额
			this.getNowUserDataSheet() //获取当前用户数据表
		},
		methods: {
			//根据日期获取默认获取当前登录用户的统计表数据
			async getNowUserDataSheet(){
				let that = this
				let postData = ""
				if(that.chooseDateUserId != 0 && that.chooseDateYear != '' && that.chooseDateMonth != ''){
					postData = {date:that.chooseDateYear + "-" + that.chooseDateMonth,userId:that.chooseDateUserId}
				}
				await this.$api.adminAct.getAdminOneDataSheet(postData)
				.then(res => {
					if(res.code == 200){
						that.nowUserDataSheet = res.data
						that.chooseDateUserId = 0,
						that.chooseDateYear   = '',
						that.chooseDateMonth  = ''
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(res.msg)
				})
			},
			//根据日期筛选获取统计表数据并赋值
			getNowUserDataSheets(index){
				let that = this
				let postData = {date:that.changeDateYear + "-" + that.changeDateMonth,userId:that.changeDateUserId}
				this.$api.adminAct.getAdminOneDataSheet(postData)
				.then(res => {
					if(res.code == 200){
						that.userDataSheetList[index].orderJson = res.data
						that.changeDateUserId = 0,
						that.changeDateYear   = '',
						that.changeDateMonth  = ''
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(res.msg)
				})
			},
			
			//获取登录用户的基本信息
			async getUserInfo(){
				let that = this
				await this.$api.adminAct.getAdminInfo()
				.then(res => {
					if(res.code == 200){
						let data = res.data
						that.userInfo.account = data.account
						that.userInfo.avatar = data.avatar
						that.userInfo.name = data.name
						that.userInfo.userId = data.id
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					uni.showLoading({
						title:res.msg
					})
					setTimeout(()=>{
						uni.hideLoading()
					},2000)
				})
			},
			//获取钱
			async getUserMoney(){
				let that = this
				await this.$api.adminAct.getAdminAssets()
				.then(res => {
					if(res.code == 200){
						let data = res.data
						 that.userInfo.extractableAssets = data.laborFeeAmount//可提取资产
						 that.userInfo.totalAssets = data.amount//总资产
						 that.userInfo.assetsId = data.id
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					uni.showLoading({
						title:res.msg
					})
					setTimeout(()=>{
						uni.hideLoading()
					},2000)
				})
			},
			queryList(pageNo, pageSize) {
				this.$api.adminAct.getAdminDataSheetList()
				.then(res => {
					console.log(res)
					if(res.code == 200){
						this.$refs.paging.complete(res.data);
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err.msg)
				})
			},
			addAccount(){
				this.isShowCreateAccount = true
			},
			submitCreate(e){
				//提交创建账号
				let postData	 =	{nickName:e.name,pwd:e.pwd,type:e.type,userName:e.account}
				this.$api.adminAct.createAdminAccount(postData)
				.then(res => {
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						this.isShowCreateAccount = false
						this.queryList(base.common_pageNum,base.common_pageSize)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			cancelCreate(){
				this.isShowCreateAccount = false
			},
			showEditPwd(userId){
				let that = this
				that.isShowEditPwd = true
				that.editUserId = userId
			},
			cancelEditPwd(){
				let that = this
				that.isShowEditPwd = false
				that.editUserId = 0
			},
			//修改密码
			submitEditPwd(e){
				let that = this
				let postData 	=	{userId:e.userId,pwd:e.newPwd,oldPwd:e.oldPwd}
				this.$api.adminAct.editAdminPwd(postData)
				.then(res => {
					if(res.code == 200){
						that.isShowEditPwd = false
						that.editUserId = 0
					}
					this.$commonJs.showToast.showNoneToast(res.msg)
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			showEditUser(userId){
				let that = this
				that.isShowEditUser = true
				that.editUserId = userId
			},
			cancelEditUser(){
				let that = this
				that.isShowEditUser = false
				that.editUserId = 0
			},
			submitEditUser(e){
				//编辑用户信息
				let postData	 =	{id:e.userId,nickName :e.name,status:e.status}
		
				this.$api.adminAct.editAdminInfo(postData)
				.then(res => {
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						this.isShowEditUser = false
						this.queryList(base.common_pageNum,base.common_pageSize)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			btnConfirm(e,userId){
				//确认时间选择
				let that = this
				e.month >= 1 && e.month <= 9 ? (e.month = "0" + e.month) : "";
				that.chooseDateUserId = userId
				that.chooseDateYear = e.year
				that.chooseDateMonth = e.month
				this.getNowUserDataSheet()
			},
			//选择列表种的时间
			btnConfirms(e,userId,itemKey){
				let that = this
				e.month >= 1 && e.month <= 9 ? (e.month = "0" + e.month) : "";
				that.changeDateUserId = userId
				that.changeDateYear = e.year
				that.changeDateMonth = e.month
				this.getNowUserDataSheets(itemKey)
			},
			btnCancel(){
				console.log('取消时间选择')
			},
			goToHome(){
				uni.reLaunch({
					url:'/pages/index/index',
					fail(err) {
						console.log(err)
					}
				})
			},
			exitAccount(){
				let that = this
				uni.showModal({
					title:"提示",
					content:"确定要退出当前账号?",
					success(res) {
						if(res.confirm == true){
							that.$api.adminAct.adminLogOut()
							.then(res => {
								if(res.code == 200){
									that.$store.dispatch('setAdminLogout')
									that.$commonJs.waitFunction.waitRelaunch('退出成功','/pages/index/index')
								}else{
									that.$commonJs.showToast.showNoneToast(err)
								}
							})
							.catch(err => {
								that.$commonJs.showToast.showNoneToast(err)
							})
						}
					}
				})
			},
			withdrawal(){
				let that = this
				if(that.userInfo.totalAssets == 0){
					this.$commonJs.showToast.showNoneToast('资产不足,继续努力哦!')
					return
				}
				let postData ={'id':that.userInfo.assetsId}
				this.$api.withdrawal.withdrawal(postData)
				.then(res => {
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						this.getUserInfo()
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			}
		},
		computed:{
			adminType(){
				return uni.getStorageSync('adminType')
			}
		}
	}
</script>

<style>

</style>
